package com.wing.sell.controller.app;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.sell.model.response.OrderSkuDetailSearchVo;
import com.wing.sell.strategy.AbstractGenerateSelOrderService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.sell.model.response.DeliveryRecordVO;
import com.wing.sell.service.DeliveryRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.sell.model.request.DeliveryRecordForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-交付记录表管理", tags = "交付记录表管理")
@RestController("appDeliveryRecordsController")
@RequestMapping("sell/app/deliveryRecords")
public class DeliveryRecordsController {

    @Autowired
    private DeliveryRecordService deliveryRecordService;
    @Autowired
    private LocaleMessage localeMessage;

    @Autowired
    private AbstractGenerateSelOrderService abstractGenerateOrderService;

    @GetMapping()
    public PageResult<DeliveryRecordVO> list(
            @RequestParam(name = "page", value = "第几页", defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit", value = "每页多少条", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort", value="排序字段名", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "desc", required = false) String order
    ) {
        Page<DeliveryRecordVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        Page<DeliveryRecordVO> list = deliveryRecordService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long deliveryRecordsId) {
        DeliveryRecordVO deliveryRecordVO = deliveryRecordService.get4appById(deliveryRecordsId);
        if (deliveryRecordVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), deliveryRecordVO);
    }

    @ApiOperation(value = "添加交付记录", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@RequestBody DeliveryRecordForm deliveryRecordForm) {
        if (deliveryRecordService.add(deliveryRecordForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }


    @ApiOperation(value = "查询-交付记录详情", notes = "详情")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @GetMapping("/detail/{id}")
    public JsonResult getDetail(@PathVariable("id") Long id) {
        DeliveryRecordForm form = deliveryRecordService.getDetailBy(id);
        return JsonResult.success(form);
    }


    @ApiOperation(value = "查询订单中物料信息", notes = "查询-订单sku")
    @PostMapping("/getSkuInfoByType")
    public JsonResult<List<OrderSkuDetailSearchVo>> getSkuInfoByType(
            @RequestParam("orderId") Long orderId,
            @RequestParam("type") String type,
            @RequestParam("warehouseId") Long warehouseId
    ) {
        List<OrderSkuDetailSearchVo> searchVos = abstractGenerateOrderService.getSelOrderList(type, orderId,warehouseId);
        return JsonResult.success(searchVos);
    }


    @ApiOperation(value = "添加交付记录(新-公共)", notes = "添加-交付记录new")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/addDeliveryInfo")
    public JsonResult addDeliveryInfo (@RequestBody DeliveryRecordForm deliveryRecordForm) {
        Integer i = abstractGenerateOrderService.generateSelOrder(deliveryRecordForm.getTypeCode(), deliveryRecordForm);
        if (i>0) {
            return JsonResult.success();
        }else{
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
    }
}
